package ldkxingzhe.l365calendar.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * l365Calendar.db 's openHelper
 * Created by ke on 16-1-30.
 */
public class L365CalendarOpenHelper extends SQLiteOpenHelper{
    @SuppressWarnings("unused")
    private static final String TAG = "L365CalendarOpenHelper";

    public static final String DA_NAME = "l365Calendar";
    public static final int VERSION = 1;

    public static final String TABLE_NOTE = "note";
    public static final String TABLE_LOCATION = "location";
    public static final String TABLE_SCHEDULE_ALARM = "schedule_alarm";
    public static final String TABLE_SCHEDULE = "schedule";

    public static final String ID = "_id";
    public static final String CREATED = "created";
    public static final String MODIFIED = "modified";
    public static final String NOTE_TITLE = "title";
    public static final String NOTE_STATE = "state";
    public static final String NOTE_PRIORITY = "priority";
    public static final String NOTE_FINISH_TIME = "finish_time";
    public static final String NOTE_DEADLINE = "deadline";
    public static final String NOTE_TAG = "tags";
    public static final String UUID = "uuid";
    private static final String CREATE_TABLE_NOTE =
            "CREATE TABLE IF NOT EXISTS " + TABLE_NOTE + "(" +
                    ID + " integer primary key autoincrement," +
                    CREATED + " integer not null," +
                    MODIFIED + " integer not null," +
            NOTE_TITLE + " text," +
            NOTE_STATE + " integer," +
            NOTE_PRIORITY + " integer," +
            NOTE_FINISH_TIME + " integer," +
            NOTE_DEADLINE + " integer," +
            NOTE_TAG + " text," +
                    UUID + " text" +
            ")";
    public static final String LOCATION_LATITUDE = "latitude";
    public static final String LOCATION_LONGITUDE = "longitude";
    public static final String LOCATION_LOCTYPE = "loctype";
    public static final String LOCATION_RADIUS = "radius";
    public static final String LOCATION_COOR_TYPE = "coor_type";
    public static final String LOCATION_PROVINCE = "province";
    public static final String LOCATION_DISTRICT = "district";
    public static final String LOCATION_STREET = "street";
    public static final String LOCATION_DESCRIBE = "location_describe";
    private static final String CREATE_TABLE_LOCATION =
            "CREATE TABLE IF NOT EXISTS " + TABLE_LOCATION + "(" +
            ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
            CREATED + " INTEGER NOT NULL," +
            LOCATION_LATITUDE + " INTEGER NOT NULL," +
            LOCATION_LONGITUDE + " INTEGER NOT NULL," +
            LOCATION_LOCTYPE + " INTEGER NOT NULL," +
            LOCATION_RADIUS + " INTEGER NOT NULL," +
            LOCATION_COOR_TYPE + " TEXT NOT NULL," +
            LOCATION_PROVINCE + " TEXT," +
            LOCATION_DISTRICT + " TEXT," +
            LOCATION_STREET + " TEXT," +
            LOCATION_DESCRIBE + " TEXT" +
            ")";

    public static final String SCHEDULE_ALARM_SCHEDULE_ID = "schedule_id";
    public static final String SCHEDULE_ALARM_BEFORE_TIME = "before_time";
    public static final String SCHEDULE_ALARM_NEXT_ALARM = "next_alarm";
    private static final String CREATE_TABLE_SCHEDULE_ALARM =
            "CREATE TABLE IF NOT EXISTS " + TABLE_SCHEDULE_ALARM +"(" +
            ID + " integer primary key autoincrement," +
            CREATED + " integer NOT NULL," +
            MODIFIED + " integer NOT NULL," +
            SCHEDULE_ALARM_SCHEDULE_ID + " integer NOT NULL," +
            SCHEDULE_ALARM_BEFORE_TIME + " integer NOT NULL," +
            SCHEDULE_ALARM_NEXT_ALARM + " integer NOT NULL" +
            ");";

    public static final String SCHEDULE_START_TIME = "start_time";
    public static final String SCHEDULE_DURATION = "duration";
    public static final String SCHEDULE_TITLE = "title";
    public static final String SCHEDULE_LOCATION = "location";
    public static final String SCHEDULE_IS_ALL_DAY = "is_all_day";
    public static final String SCHEDULE_IS_REPEAT_FINISHED = "is_repeat_finished";
    public static final String SCHEDULE_DESCRIPTION = "description";
    public static final String SCHEDULE_COMPLETED = "completed";
    public static final String SCHEDULE_REPEAT_TYPE = "repeat_type";
    public static final String SCHEDULE_REPEAT_STOP_TIME = "repeat_stop_time";
    public static final String SCHEDULE_REPEAT_COUNT = "repeat_count";
    public static final String SCHEDULE_REPEAT_FREQUENCY = "repeat_frequency";
    private static final String CREATE_TABLE_SCHEDULE =
            "CREATE TABLE IF NOT EXISTS " + TABLE_SCHEDULE +"(" +
            ID + " integer primary key autoincrement," +
            CREATED + " integer NOT NULL," +
            MODIFIED + " integer NOT NULL," +
            SCHEDULE_START_TIME + " integer NOT NULL," +
            SCHEDULE_DURATION + " integer," +
            SCHEDULE_TITLE + " text NOT NULL," +
            SCHEDULE_LOCATION + " text," +
            SCHEDULE_DESCRIPTION + " text," +
            SCHEDULE_COMPLETED + " integer default 0," +
            SCHEDULE_REPEAT_TYPE + " integer default 0," +
            SCHEDULE_IS_ALL_DAY + " integer default 0," +
            SCHEDULE_IS_REPEAT_FINISHED + " integer default 0," +
            SCHEDULE_REPEAT_STOP_TIME + " integer," +
            SCHEDULE_REPEAT_COUNT + " integer," +
            SCHEDULE_REPEAT_FREQUENCY + " integer," +
            UUID + " text" +
            ");";

    private static L365CalendarOpenHelper mInstance;
    synchronized public static L365CalendarOpenHelper getInstance(Context context){
        if(mInstance == null){
            mInstance = new L365CalendarOpenHelper(context);
        }
        return mInstance;
    }

    private L365CalendarOpenHelper(Context context) {
        super(context, DA_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_NOTE);
        db.execSQL(CREATE_TABLE_LOCATION);
        db.execSQL(CREATE_TABLE_SCHEDULE_ALARM);
        db.execSQL(CREATE_TABLE_SCHEDULE);
        Log.v(TAG, "onCreate, database was created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String dropStr = "DROP TABLE IF IT EXIST ";
        db.execSQL(dropStr + TABLE_NOTE);
        db.execSQL(dropStr + TABLE_LOCATION);
        db.execSQL(dropStr + TABLE_SCHEDULE_ALARM);
        db.execSQL(dropStr + TABLE_SCHEDULE);
        onCreate(db);
    }
}
